Eye_tracking 1
 
Cargando...
Buscando...
Nada coincide
gaze_estimator.py
Ir a la documentación de este archivo.
1from loguru import logger
2from typing import List
3
4import numpy as np
5import torch
6from omegaconf import DictConfig
7
8from .common.camera import Camera
9from .common.face import Face
10from .common.face_parts import FacePartsName
11from .head_pose_estimation.head_pose_normalizer import HeadPoseNormalizer
12from .head_pose_estimation.face_landmark_estimator import LandmarkEstimator
13from .models import create_model
14from .transforms import create_transform
15from .common.face_model_mediapipe import FaceModelMediaPipe
16
18 EYE_KEYS = [FacePartsName.REYE, FacePartsName.LEYE]
19
20 def __init__(self, config: DictConfig):
21 self._config = config
22
23 self._face_model3d = FaceModelMediaPipe()
24
25 self.camera = Camera(config.gaze_estimator.camera_params)
26 self._normalized_camera = Camera(config.gaze_estimator.normalized_camera_params)
27
28 self._landmark_estimator = LandmarkEstimator(config)
29 self._head_pose_normalizer = HeadPoseNormalizer(
30 self.camera, self._normalized_camera,
31 self._config.gaze_estimator.normalized_camera_distance)
33 self._transform = create_transform(config)
34
35 def _load_model(self) -> torch.nn.Module:
36 model = create_model(self._config)
37 checkpoint = torch.load(self._config.gaze_estimator.checkpoint,
38 map_location='cpu')
39 model.load_state_dict(checkpoint['model'])
40 model.to(torch.device(self._config.device))
41 model.eval()
42 return model
43
44 def detect_faces(self, image: np.ndarray) -> List[Face]:
45 return self._landmark_estimator.detect_faces(image)
46
47 def estimate_gaze(self, image: np.ndarray, face: Face) -> None:
48 self._face_model3d.estimate_head_pose(face, self.camera)
49 self._face_model3d.compute_3d_pose(face)
50 self._face_model3d.compute_face_eye_centers(face, self._config.mode)
51 self._head_pose_normalizer.normalize(image, face)
52 self._run_ethxgaze_model(face)
53
54 @torch.no_grad()
55 def _run_ethxgaze_model(self, face: Face) -> None:
56 image = self._transform(face.normalized_image).unsqueeze(0)
57
58 device = torch.device(self._config.device)
59 image = image.to(device)
60 prediction = self._gaze_estimation_model(image)
61 prediction = prediction.cpu().numpy()
62
63 face.normalized_gaze_angles = prediction[0]
64 face.angle_to_vector()
65 face.denormalize_gaze_vector()
None estimate_gaze(self, np.ndarray image, Face face)
__init__(self, DictConfig config)
List[Face] detect_faces(self, np.ndarray image)
None _run_ethxgaze_model(self, Face face)